package com.license.common.excellParser;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.license.common.util.Constants;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ReadExcel {

	 private String inputFile;
	 private Sheet sheet;
	 
	 public ReadExcel() throws IOException{
		 this.inputFile = Constants.EXTERN_EXCELL_PATH + "test" + Constants.EXCELL_EXTENSION;
		 this.initialize();
	 }
	 
	 public ReadExcel(String inputFile) throws IOException{
		 this.inputFile = inputFile;
		 this.initialize();
	 }
	 
	  public void setInputFile(String inputFile) {
	    this.inputFile = inputFile;
	  }
	  
	  public void initialize() throws IOException{
		  File inputWorkbook = new File(inputFile);
		    Workbook w;
		    try {
		      w = Workbook.getWorkbook(inputWorkbook);
		      // Get the first sheet
		      this.sheet = w.getSheet(0);
		      // Loop over first 10 column and lines
		
		    } catch (BiffException e) {
		      e.printStackTrace();
		    }
		   
	  }

	  public Double readValue(int column, int raw){
		  return Double.parseDouble(this.sheet.getCell(column, raw).getContents());
	  }
	  
	  public List<Integer> readAllRawValues(int raw){
		  List<Integer> rawValues = new ArrayList<Integer>();
	        for (int i = 0; i < sheet.getColumns(); i++) {
	          rawValues.add(Integer.parseInt(sheet.getCell(i, raw).getContents()));
	        }
		  return rawValues;
	  }
	  
	  public List<Double> readAllColumnValues(int column){
		  List<Double> columnValues = new ArrayList<Double>();
	        for (int i = 2; i < sheet.getRows(); i++) {
	          columnValues.add(Double.parseDouble(sheet.getCell(column, i).getContents()));
	        }
		  return columnValues;
	  }
	  
	  public List<Double> readColumnValues(int column, int startIndex, int endIndex){
		  List<Double> columnValues = new ArrayList<Double>();
		  endIndex = endIndex < this.sheet.getRows()? endIndex : this.sheet.getRows();
	        for (int i = startIndex; i < endIndex; i++) {
	          columnValues.add(Double.parseDouble(sheet.getCell(column, i).getContents()));
	        }
		  return columnValues;
	  }

}
